<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let arr = ['apple', 'orange', 'banana'];
            console.log(arr.toString()); // apple,orange,banana

            Array.prototype.myToString = function () {
                let str = '';
                for (let i = 0; i < this.length; i++) {
                    // 数组元素为undedined或者为null是置为空
                    this[i] = typeof this[i] === 'undefined' || this[i] === null ? '' : this[i];
                    if (i == this.length - 1) {
                        return (str = str + arr[i]);
                    }
                    str = str + arr[i] + ',';
                }
                return str;
            };

            console.log(arr.myToString()); // apple,orange,banana
        </script>
    </body>
</html>
